
function showPaymentTypes(){
     
     $('input[id^=collection_pay_type_]').delegate('','click', function() {
            showChequeFields();
    });
     
}


function showChequeFields(){
   if ($('#collection_pay_type_cheque:checked').length > 0 ){
        $('#chequeDiv1').show();
        $('#chequeDiv2').show();
    }else{
        $('#chequeDiv1').hide();
        $('#chequeDiv2').hide();
    }
}

function addCollection(type,showSucessBar){
      var url = $("#url4AddCollection").val();
      if(!type){
        $.ajax ({
            type: "GET",
            url: url,
            success: function (html)
            {
                $('#DAH').html(html);
                showPaymentTypes();
                showChequeFields();
                $(".chzn-select").chosen();
                $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
                if(showSucessBar)
                    $('#DAH').prepend('<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong> Collection added successfully. </p></div>');     
            }
        });
      }else{
          var data = $("#form").serialize()
        $.ajax ({
            type: "POST",
            url: url,
            data :data,
            success: function (html)
            {
                if(html == "Done"){
                    addCollection(false, true);
                    $('html, body').animate({scrollTop:$('#rightSide').position().top}, 'slow');
                }
                else{
                    $('#DAH').html(html);
                    $(".chzn-select").chosen();
                    $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
                    showPaymentTypes();
                    showChequeFields();
                }
            }
        });
      }
}




function editCollection(type,showSucessBar,id){
      var url = $("#url4EditCollection").val();
      if(!type){
        $.ajax ({
            type: "GET",
            url: url,
            data: "id=" + id,
            success: function (html)
            {
                $('#DAH').html(html);
                showPaymentTypes();
                showChequeFields();
                $(".chzn-select").chosen();
                $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
                if(showSucessBar)
                    $('#DAH').prepend('<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong> Collection edited successfully. </p></div>');     
            }
        });
      }else{
          var data = $("#form").serialize()
        $.ajax ({
            type: "POST",
            url: url,
            data :data + "&id=" + id,
            success: function (html)
            {
                if(html == "Done"){
                    addCollection(false, true);
                    $('html, body').animate({scrollTop:$('#rightSide').position().top}, 'slow');
                }
                else{
                    $('#DAH').html(html);
                    $(".chzn-select").chosen();
                    $(".datepicker").datepicker({ dateFormat: 'yy-mm-dd' });
                    showPaymentTypes();
                    showChequeFields();
                }
            }
        });
      }
}



